﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Weight.Base;

namespace Weight.Controls
{
    public class LYFDataGridView : DataGridView
    {
        public LYFDataGridView()
        {
            this.SetStyle(
                 //ControlStyles.UserPaint |//使用自定义的绘制方式
                 ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                | ControlStyles.Opaque
                , true);

            AllowUserToAddRows = false;
            AllowUserToResizeRows = false;
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            BackgroundColor = Color.FromArgb(0, 163, 129);
            EnableHeadersVisualStyles = false;
            ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            ColumnHeadersDefaultCellStyle.Padding = new Padding(5);
            ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ActiveCaption;
            ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
            ColumnHeadersDefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
            ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White;
            ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            ColumnHeadersHeight = 40;

            DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            DefaultCellStyle.BackColor = SystemColors.Window;
            DefaultCellStyle.ForeColor = SystemColors.ControlText;
            DefaultCellStyle.SelectionBackColor = SystemColors.GradientActiveCaption;
            DefaultCellStyle.SelectionForeColor = SystemColors.MenuText;

            GridColor = SystemColors.GradientInactiveCaption;

            MultiSelect = false;
            ReadOnly = true;
            RowHeadersVisible = false;
            RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;

            RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            RowsDefaultCellStyle.BackColor = SystemColors.Control;

            RowTemplate.Resizable = DataGridViewTriState.True;
            SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor;
            base.OnCellFormatting(e);
        }

        public void SetSkin(Color color)
        {
            ColumnHeadersDefaultCellStyle.BackColor = color;
            RowsDefaultCellStyle.SelectionBackColor = Colors.ColorChangedLightColour(color, 0.8f);
        }
    }
}
